package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import com.glory.cqaospda.R
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.viewmodel.QTimeModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.JsonObject
import com.pda.platform.ui.ui_pdaplatform.datepicker.CustomDatePicker
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils
import kotlinx.android.synthetic.main.activity_qtime.*
import kotlinx.android.synthetic.main.activity_title.*

class QTimeActivity : BaseActivity<QTimeModel>(){

    var mDatePicker: CustomDatePicker? = null
    var beginDate: String? = null

    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when(msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    intent.setClass(this@QTimeActivity, clazz)
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }

    override fun layoutId(): Int {
        return R.layout.activity_qtime
    }

    override fun initData() {

        flBeginTimeList.setOnClickListener {
            mDatePicker!!.show(System.currentTimeMillis())
        }

        tvVacuumIn.setOnClickListener {
            if (etLotID.text.toString() == ""){
                viewModel.defUI.toastEvent.value = ToastMessageBean("批次号不能为空",false)
                return@setOnClickListener
            }
            if (etEqpID.text.toString() == ""){
                viewModel.defUI.toastEvent.value = ToastMessageBean("设备号不能为空",false)
                return@setOnClickListener
            }
            val map = JsonObject()
            map.addProperty("LotNo",etLotID.text.toString())
            map.addProperty("Vacuum_In_Out",0)
            map.addProperty("EquipmentName",etEqpID.text.toString())
            map.addProperty("ActionTime",if (tvBeginTime.text.toString() == "") System.currentTimeMillis()
                .toString() else tvBeginTime.text.toString())
            viewModel.defaultRepairList(map)
        }
        tvVacuumOut.setOnClickListener {
            if (etLotID.text.toString() == ""){
                viewModel.defUI.toastEvent.value = ToastMessageBean("批次号不能为空",false)
                return@setOnClickListener
            }
            if (etEqpID.text.toString() == ""){
                viewModel.defUI.toastEvent.value = ToastMessageBean("设备号不能为空",false)
                return@setOnClickListener
            }
            val map = JsonObject()
            map.addProperty("LotNo",etLotID.text.toString())
            map.addProperty("Vacuum_In_Out",1)
            map.addProperty("EquipmentName",etEqpID.text.toString())
            map.addProperty("ActionTime",if (tvBeginTime.text.toString() == "") System.currentTimeMillis()
                .toString() else tvBeginTime.text.toString())
            viewModel.defaultRepairList(map)
        }

        viewModel.defUI.callObserve.observe(this@QTimeActivity) {
            if (!Constant.okgoError) {
                when (it.type) {
                    0 -> {
                        viewModel.defUI.toastEvent.value = ToastMessageBean("操作成功", true)
                        finish()
                    }
                }
            }else{
                Constant.okgoError = false
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {

        FreeApi_ActivityManager.getActivityManager().addActivity(this)
        tvBaseSubmit.visibility = View.GONE
        tvTitle.text = "QTIME"
        val instance = MyApp.instance
        instance!!.handler = handler
        initDatePicker()
    }

    private fun initDatePicker() {
        val beginTimestamp = FreeApi_DateUtils.str2Long("1992-03-20 hh:mm:ss", false)
        val endTimestamp = FreeApi_DateUtils.str2Long("2222-02-22 hh:mm:ss", false)

        // 通过时间戳初始化日期，毫秒级别
        mDatePicker = CustomDatePicker(this, { timestamp ->

            beginDate = FreeApi_DateUtils.long2Str(timestamp, true)
            tvBeginTime.text = beginDate

        }, beginTimestamp, endTimestamp)
        // 不允许点击屏幕或物理返回键关闭
        mDatePicker!!.setCancelable(true)
        // 不显示时和分
        mDatePicker!!.setCanShowPreciseTime(true)
        // 不允许循环滚动
        mDatePicker!!.setScrollLoop(false)
        // 不允许滚动动画
        mDatePicker!!.setCanShowAnim(false)
    }
}